/************************************************************/
/*															*/
/* $File      :  d3d_obj.cpp								*/
/* $Author    :  Sean Franklin								*/
/* $Info      :  See d3d_obj.h  							*/
/*															*/
/************************************************************/

#include "d3d_obj.h"


void XCT_D3DOBJ::Begin(void)
{
	BeginScene();
	BeginSprite();
}

void XCT_D3DOBJ::End(void)
{
	EndSprite();
	EndScene();
	Present();
}

void XCT_D3DOBJ::InitXCT_D3D(HWND hWnd)
{
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	
	D3DPRESENT_PARAMETERS d3dpp;
	
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE; 
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.BackBufferCount = 0;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	
	HRESULT _res;

	_res = d3d->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			hWnd,
			D3DCREATE_HARDWARE_VERTEXPROCESSING,
			&d3dpp,
			&d3dDevice);
	if ( _res != D3D_OK )
	{
		d3d->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING,
			&d3dpp,
			&d3dDevice);
	}
	
	D3DXCreateSprite(d3dDevice, &d3dSpt);
}


BOOL XCT_D3DOBJ::LoadSprite(char *FileName, D3DCOLOR ColorKey)
{
	if ( !FileName )
		return -1;

	XCTBitmap temp;

	temp.szFileName = FileName;
	
	D3DXCreateTextureFromFileExA(d3dDevice,
		FileName,
		0,
		0,
		D3DX_DEFAULT,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		ColorKey,
		&temp.ii,
		0,
		&temp.sprite);

	if ( !temp.sprite )
	{
		char *temp = new char[200];
		sprintf_s(temp, 200, "Failed to load sprite \"%s\"", FileName);
		delete [] temp; temp = 0;
		return -1;
	}


	m_vImgs.push_back(temp);

	return (m_vImgs.size() - 1);
}


bool XCT_D3DOBJ::RenderSprite(int id, float x, float y, D3DCOLOR color)
{
	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 position(x, y, 0.0f);
	
	float scaleX = 1, scaleY = 1, scaleZ = 1;
	D3DXMATRIX matScale;
	

	if(m_vImgs[id].sprite == NULL)
	{
		MessageBox(NULL, "ERROR [RenderSprite()]: Trying to output with an invalid image ID.", "", 0);
		exit(0);
	}
	else
	{
		D3DXMatrixScaling( &matScale, scaleX, scaleY, scaleZ);
		d3dDevice->SetTransform( D3DTS_VIEW, &matScale);
		d3dSpt->Draw(m_vImgs[id].sprite, NULL, &center, &position, color);

		return true;
	}
	return false;
}

// width + (8 - (width % 8) )

bool XCT_D3DOBJ::RenderFrame(int id, float x, float y, RECT SrcRect, D3DCOLOR color)
{
	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 position(x, y, 0.0f);
	
	if(m_vImgs[id].sprite == NULL)
	{
		MessageBox(NULL, "ERROR [RenderFrame()]: Trying to output with an invalid image ID.", "", 0);
		exit(0);
	}
	else
	{
		d3dSpt->Draw(m_vImgs[id].sprite, &SrcRect, &center, &position, color);
		return true;
	}
	return false;
}

void XCT_D3DOBJ::UnloadSprite(int id)
{
	m_vImgs[id].sprite->Release();
	m_vImgs[id].sprite = 0;
	m_vImgs[id].szFileName = NULL;
	return;
}

void XCT_D3DOBJ::UnloadAllSprites(void)
{
	for ( UINT i = 0; i < m_vImgs.size(); ++i )
	{
		if ( m_vImgs[i].sprite != NULL )
		{
			m_vImgs[i].sprite->Release();
			m_vImgs[i].sprite = 0;
			m_vImgs[i].szFileName = NULL;
		}
	}

	d3dDevice->Release();
	d3d->Release();
	d3dSpt->Release();
	return;
}

void XCT_D3DOBJ::ClearColor(D3DCOLOR color)
{
	d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, color, 1.0f, 0);
}


int XCT_D3DOBJ::LoadFont(INT Height, UINT Weight, BOOL Italic, LPCTSTR pFontName)
{
	if ( !pFontName )
	{
		MessageBox(NULL, "Could not load font: No name provided.", "ERROR", MB_ICONERROR);
		exit(0);
	}

	XCTFont temp;

	temp.szFontName = pFontName;

	D3DXCreateFont(d3dDevice, 
               Height, 0,  
               Weight,    
               1,   
               Italic,   
               DEFAULT_CHARSET,    
               OUT_DEFAULT_PRECIS,  
               DEFAULT_QUALITY,   
               DEFAULT_PITCH || FF_DONTCARE,    
               pFontName,   
               &temp.font); 

	if ( !temp.font )
	{
		char *buff = 0;
		sprintf(buff, "Could not load font \"%s\"", pFontName);
		MessageBox(NULL, buff, "ERROR", MB_ICONERROR);
		exit(0);
	}

	m_vFonts.push_back(temp);

	return (m_vFonts.size() - 1);
}

bool XCT_D3DOBJ::RenderText(FONTID id, LPCTSTR pText, int x, int y, D3DCOLOR color)
{
	RECT pos = {x, y, SCREEN_WIDTH, SCREEN_HEIGHT};

	if ( !pText )
		return false;

	if ( m_vFonts[id].font )
			m_vFonts[id].font->DrawText(d3dSpt,
				 pText,
				 strlen(pText),
				 &pos,
				 DT_LEFT,
				 color);

	return true;
}


void XCT_D3DOBJ::UnloadFont(FONTID id)
{
	m_vFonts[id].font->Release();
	m_vFonts[id].font = 0;
}

void XCT_D3DOBJ::UnloadAllFonts(void)
{
	for ( unsigned int i = 0; i < m_vFonts.size(); ++i )
	{
		if ( m_vFonts[i].font )        {
			m_vFonts[i].font->Release();
			m_vFonts[i].font = 0;       }
	}
}
